#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : db_model.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-09-05 10:28 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from report_system.utils import date_util
from report_system.utils.enum_util import NOT_DEL
from report_system.other.settlement.models.base_model import Users
from report_system.other.settlement.models.base_model import LodHouse
from report_system.other.settlement.models.base_model import InvestCompactHouse
from report_system.other.settlement.models.base_model import InvestRelationRecord


class InvestRelationRecordModel(InvestRelationRecord):
    def __init__(self, house_id=None, invest_id=None):
        self._house_id = house_id
        self._invest_id = invest_id

    # @property
    # def house_id(self):
    #     return self._house_id
    #
    # @house_id.setter
    # def house_id(self, value):
    #     self._house_id = value
    #
    # @property
    # def invest_id(self):
    #     return self._invest_id
    #
    # @invest_id.setter
    # def invest_id(self, value):
    #     self._invest_id = value

    def get_all_invest_id(self):
        items = self.query.filter_by(is_delele=NOT_DEL).order_by('create_time').all()
        return {item.invest_id for item in items}

    def get_by_invest_id(self):
        items = self.query.filter_by(
            invest_id=self._invest_id,
            is_delele=NOT_DEL
        ).order_by('create_time').all()
        for item in items:
            item.house_id = item.lod_house_id
            item.date_relieve = item.date_relieve  # if item.date_relieve else date_util.datetime_now()
            yield item

    def get_by_house_id(self):
        items = self.query.filter_by(
            lod_house_id=self._house_id,
            is_delele=NOT_DEL
        ).order_by('create_time').all()
        for item in items:
            item.house_id = item.lod_house_id
            item.date_relieve = item.date_relieve  # if item.date_relieve else date_util.datetime_now()
            yield item

    def get(self):
        items = self.query.filter_by(
            is_delele=NOT_DEL
        ).order_by('create_time').all()
        for item in items:
            item.house_id = item.lod_house_id
            item.date_relieve = item.date_relieve  # if item.date_relieve else date_util.datetime_now()
            yield item

    def get_by_invest_id_house_id(self):
        items = self.query.filter_by(
            invest_id=self._invest_id,
            lod_house_id=self._house_id,
            is_delele=NOT_DEL
        ).order_by('create_time').all()
        for item in items:
            item.house_id = item.lod_house_id
            item.date_relieve = item.date_relieve  # if item.date_relieve else date_util.datetime_now()
            yield item


class UsersModel(Users):
    def __init__(self, user_id=None, username=None):
        self._user_id = user_id
        self._username = username

    # @property
    # def user_id(self):
    #     return self._user_id
    #
    # @user_id.setter
    # def user_id(self, value):
    #     self._user_id = value
    #
    # @property
    # def username(self):
    #     return self._username
    #
    # @username.setter
    # def username(self, value):
    #     self._username = value

    def get_by_user_id(self):
        return self.query.filter_by(id=self._user_id).first()


class LodHouseModel(LodHouse):
    def __init__(self, house_id=None):
        self._house_id = house_id

    # @property
    # def house_id(self):
    #     return self._house_id
    #
    # @house_id.setter
    # def house_id(self, value):
    #     self._house_id = value
    #
    # @house_id.getter
    # def house_id(self):
    #     return self._house_id

    def get_by_house_id(self):
        items = self.query.filter_by(id=self._house_id).all()
        for item in items:
            item.house_id = item.id
            yield item


class InvestCompactHouseModel(InvestCompactHouse):
    def __init__(self, house_id=None, invest_id=None):
        self._house_id = house_id
        self._invest_id = invest_id

    def get_by_house_id(self):
        return self.query.filter_by(house_id=self._house_id).first()

    def get_by_invest_id(self):
        items = self.query.filter_by(invest_id=self._invest_id).all()
        for item in items:
            yield item

    def get_by_invest_id_house_id(self):
        return self.query.filter_by(invest_id=self._invest_id, house_id=self._house_id).first()

# house = HouseModel(house_id=3333)
# # house.house_id = 123
# print(house.house_id)
#
# def test():
#     invest_compact_house_model = InvestCompactHouseModel(house_id=241887713387638778)
#     for invest_compact_house in invest_compact_house_model.get_by_house_id():
#         #  lease_date_begin = Column(Date, comment='租赁开始日期（房屋承租期限，开始日期')
#         #     lease_date_end = Column(Date, comment='租赁结束日期（房屋承租期限，结束日期')
#         print(invest_compact_house.lease_date_begin)
#         print(invest_compact_house.lease_date_end)
#
# #
# if __name__ == '__main__':
#     test()
